Grokking Microservices Design Patterns
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
The Course at a Glance
Strangler Fig Pattern
Introduction
The Problem: Legacy Systems
The Strangler Pattern: A Solution
The Architecture of the Strangler Pattern
Strangler Pattern: A Detailed Example
Key Insights and Implications
API Gateway Pattern
Introduction to the API Gateway Pattern
Advantages of API Gateway Pattern
API Gateway Pattern: An Example
Performance Implications
System Design Example
Backends for Frontends (BFF) Pattern
Introduction to BFF
The Problem: Traditional Backend Models
The Architecture of the BFF Pattern
BFF Pattern: An Example
Performance Implications
System Design Examples
Service Discovery Pattern
What is Service Discovery Pattern?
The Problem: Service Coordination in Distributed Systems
Service Discovery Pattern: A Solution
The Architecture of the Service Discovery Pattern
The Inner Workings of the Service Discovery Pattern
Service Discovery Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Security Considerations
Circuit Breaker Pattern
Introduction
The Problem: The Struggles of Distributed Systems and Service Failures
The Circuit Breaker Pattern: An Effective Shield Against Cascading Failures
Circuit Breaker Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Summary
Bulkhead Pattern
Introduction
The Problem: Failure Propagation in Distributed Systems
The Bulkhead Pattern: A Solution
The Architecture
The Inner Workings
Bulkhead Pattern: A Example
Performance Implications and Special Considerations
System Design Examples
Conclusion
Retry Pattern
Introduction
The Retry Pattern: A Solution to Unreliable External Resources
The Architecture of the Retry Pattern
Retry Pattern: An Example
Performance Implications
Use Cases and System Design Examples
Conclusion
Sidecar Pattern
Introduction to the Sidecar Pattern
The Problem: Monolithic Application Management
A Solution to the Monolithic Mayhem
The Architecture of the Sidecar Pattern
Sidecar Pattern: Bringing Theory to Practice with an Example
Performance Implications
System Design Examples: Bringing the Sidecar Pattern to Life
Saga Pattern
Introduction to Saga Pattern
The Problem: Traditional Transaction Models
The Saga Pattern: A Solution
The Architecture of the Saga Pattern
The Inner Workings of the Saga Pattern
Saga Pattern: A Example
Performance Implications
System Design Examples
Conclusion
Introduction
The Problem: Managing Complex Interactions in Distributed Systems
Event-Driven Architecture: A Promising Solution
The Architecture of the Event-Driven Architecture Pattern
The Inner Workings of the Event-Driven Architecture Pattern
Event-Driven Architecture Pattern: An Example
Performance Implications and Special Considerations
Use Cases and System Design Examples
Conclusion
Introduction
The Problem: Traditional CRUD Operations
CQRS Pattern: A Solution
The Architecture of the CQRS Pattern
The Inner Workings of the CQRS Pattern
CQRS Pattern: An Example
Issues, Special Considerations, and Performance Implications
System Design Examples
Introduction
The Problem: Configuration Management in a Microservices Architecture
The Solution: Configuration Externalization Pattern
Unveiling the Architecture: How Does Configuration Externalization Work?
Delving into Code: An Example
Considerations and Implications
Use Cases and Real-world Examples
Conclusion
Embrace the Future of Software Architecture
Saga Pattern: A Example

Understanding the concept is great, but seeing it in action is even better! Let's now illustrate the Saga Pattern using Java. We will create a simple online shopping scenario, involving two services: the Order Service and the Inventory Service. Ready to turn theory into code? Let's go!

Setting the Stage: The Scenario

Picture yourself shopping online. You add items to your cart and proceed to checkout, creating an order. The Inventory Service then checks if the items are available. If yes, it updates the inventory, and the order is completed. If not, the order is cancelled. Seems simple, right? But under the hood, it's a perfect use case for the Saga Pattern.

The Actors: Defining Services

We will first define our services. Both the Order Service and the Inventory Service will have their local databases, adhering to the database per service principle. Let's first look at the Order Service:

public class OrderService { private OrderDatabase orderDatabase = new OrderDatabase(); public void createOrder(Order order) { orderDatabase.save(order); //... } public void cancelOrder(String orderId) { //... } }

The createOrder method saves the order in the database. The cancelOrder method will be used for our compensating transaction if the inventory check fails.

Now, let's define the Inventory Service:

public class InventoryService { private InventoryDatabase inventoryDatabase = new InventoryDatabase(); public boolean checkAndReduceInventory(Order order) { //... } public void addBackInventory(Order order) { //... } }

The checkAndReduceInventory method checks the inventory and reduces it if the items are available. The addBackInventory method will serve as our compensating transaction.

The Conductor: The Orchestrator

In our example, we will use an Orchestration Saga, where an orchestrator directs the sequence of transactions. Here's our orchestrator:

public class OrderOrchestrator { private OrderService orderService = new OrderService(); private InventoryService inventoryService = new InventoryService(); public void processOrder(Order order) { //... } }

The processOrder method is where the saga takes place. It first creates an order, then checks and reduces the inventory. If the inventory check fails, it cancels the order.

The Saga in Action: The Process Method

Here's how the processOrder method would look:

public void processOrder(Order order) { orderService.createOrder(order); if (!inventoryService.checkAndReduceInventory(order)) { orderService.cancelOrder(order.getId()); } }

And there you have it! The Saga Pattern in a nutshell. But our journey doesn't end here.

Expecting the Unexpected: Handling Failures

You might be thinking, "What if something goes wrong? What if a service fails in the middle of the saga?" That's a great question and this is where the Saga Log comes in.

We would need to expand our services to record the steps in the saga. If a service fails, we can refer to the Saga Log, see where it left off, and execute the necessary compensating transactions. This could be an excellent exercise for you to implement and truly understand the Saga Pattern's robustness.

And Cut: Wrapping Up

We have now seen a complete Java example of the Saga Pattern, demonstrating how it can maintain data consistency in a distributed system. Wasn't that a fun journey from online shopping to coding?

However, this is only a basic illustration. In a real-world application, there would

be more complexity, more services involved, and more potential for failures. But don't be intimidated! The Saga Pattern is well-equipped to handle these complexities. It allows us to split a large transaction into manageable, independent parts that can be executed and compensated for separately.

But as with everything, the Saga Pattern is not a silver bullet. There are considerations and implications when using it. And that is exactly what we will explore next. Are you ready to dig deeper into the world of the Saga Pattern? Let's jump right in!

The Inner Workings of the Saga Pattern
Performance Implications
Mark as Completed

On this page

Setting the Stage: The Scenario

The Actors: Defining Services

The Conductor: The Orchestrator

The Saga in Action: The Process Method

Expecting the Unexpected: Handling Failures

And Cut: Wrapping Up